import MyComponent from "../common/MyComponent";
import Core from "../Core";
import { LoadingSystem, LoadingTask } from "./LoadingSystem";

const { ccclass, property } = cc._decorator;

@ccclass
export default class LoadingView extends MyComponent {
  private progress_loading: cc.ProgressBar = null;
  //private lbl_progress: cc.Label = null;
  //private node_zhuan: cc.Node = null;
  //private originXOfZhuan: number = 0;

  constructor() {
    super();
  }

  private age_tips: cc.Node = null;
  protected onLoad(): void {
    Core.init();
    //不显示调试信息
    cc.debug.setDisplayStats(false);
  }

  //加载并缓存资源-- 根据实际需求在中间补充
  private readonly loadingTasks: LoadingTask[] = [
    { weight: 0.1, description: "游戏配置数据完成", boundleName: "json" },
    { weight: 0.3, description: "基础预制体完成", boundleName: "prefabs" },
    { weight: 0.5, description: "纹理资源加载完成", boundleName: "textures" },
    { weight: 0.6, description: "配置文件加载完成", boundleName: "config" },
    { weight: 0.95, description: "音频资源加载完成", boundleName: "sounds" },
  ];

  private updateProgress(progress: number, desc: string) {
    this.progress_loading.progress = progress;
    // this.node_zhuan.x =
    //   this.originXOfZhuan + this.progress_loading.totalLength * progress;
    //this.lbl_progress.string = `${(progress * 100).toFixed(2)}% \n${desc} `;
  }
  async start() {
    //this.originXOfZhuan = this.node_zhuan.x;
    await LoadingSystem.instance.runSequence(
      this.loadingTasks,
      this.updateProgress.bind(this)
    );
    //加载并初始化json数据
    await Core.JSONData.ensureLoaded();
    //进入登录场景
    cc.director.loadScene("loginScene");
  }

  // onBtnClick(event) {
  //   //Core.audioManager.playButtonClicked();
  //   switch (event.target.name) {
  //     case "btn_age":
  //       this.age_tips.active = true;
  //       break;
  //   }
  // }

  // update (dt) {}
}
